<?php
/**
 * User: fdg348
 * Date: 2018/2/10
 * Time: 12:00
 */
namespace backend\controllers;

use yii\web\AdminController;

class MenuSynchronizationController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'MenuSynchronization';
    
    public $host = '202.1.100.47';
    
    public $username = 'root';
    
    public $password = '111111';
    
    public $test_db_name = 'j_db_test';
    
//     public $preview_db_name = 'j_db_test_back';//back库用于测试
    public $preview_db_name = 'jd_preview';
    
    public function actionIndex()
    {
        $con = mysql_connect($this->host,$this->username,$this->password) or die('can not connect server!');
        //链接测试库
        mysql_select_db($this->test_db_name, $con);
        mysql_query("set names 'utf8'");
        $find_menu = mysql_query('SELECT * FROM j_menu',$con);
        //$test_menu_arr：测试环境中的菜单数据
        $test_menu = $test_menu_arr = [];
        while($row = mysql_fetch_array($find_menu)){
            if (count($row) > 0)foreach ($row as $row_key=>$row_val){;
                if (is_integer($row_key)){
                    continue;
                }
                $test_menu[$row_key] = $row_val;
            }
            $test_menu_arr[] = $test_menu;
        }
        $find_auth_rule = mysql_query('SELECT * FROM j_auth_rule',$con);
        //$test_auth_rule_arr：测试环境中的权限数据
        $test_auth_rule = $test_auth_rule_arr = [];
        while($row_a = mysql_fetch_array($find_auth_rule)){
            if (count($row_a) > 0)foreach ($row_a as $row_a_key=>$row_a_val){;
                if (is_integer($row_a_key)){
                    continue;
                }
                $test_auth_rule[$row_a_key] = $row_a_val;
            }
            $test_auth_rule_arr[] = $test_auth_rule;
        }
        //链接预发布库
        mysql_select_db($this->preview_db_name, $con);
        $find_menu = mysql_query('SELECT * FROM j_menu',$con);
        //$preview_menu_arr：预发布环境中的菜单数据
        $preview_menu = $preview_menu_arr = [];
        while($row = mysql_fetch_array($find_menu)){
            if (count($row) > 0)foreach ($row as $row_key=>$row_val){;
            if (is_integer($row_key)){
                continue;
            }
            $preview_menu[$row_key] = $row_val;
            }
            $preview_menu_arr[] = $preview_menu;
        }
        $find_auth_rule = mysql_query('SELECT * FROM j_auth_rule',$con);
        //$preview_auth_rule_arr：预发布环境中的权限数据
        $preview_auth_rule = $preview_auth_rule_arr = [];
        while($row_a = mysql_fetch_array($find_auth_rule)){
            if (count($row_a) > 0)foreach ($row_a as $row_a_key=>$row_a_val){;
            if (is_integer($row_a_key)){
                continue;
            }
            $preview_auth_rule[$row_a_key] = $row_a_val;
            }
            $preview_auth_rule_arr[] = $preview_auth_rule;
        }
        //将测试库中的数据同步到预发布环境
        $preview_menu_arr_id = array_column($preview_menu_arr, 'id');
        if (is_array($test_menu_arr) && count($test_menu_arr) > 0)foreach ($test_menu_arr as $test_menu_arr_val){
            $sql = '';
            if (in_array($test_menu_arr_val['id'], $preview_menu_arr_id)){
                $id = $test_menu_arr_val['id'];
                $sql = "UPDATE j_menu SET ";
                if (is_array($test_menu_arr_val) && count($test_menu_arr_val) > 0)foreach ($test_menu_arr_val as $menu_key => $menu_val){
                    if ($menu_key == 'id'){
                        continue;
                    }
                    $sql .= $menu_val !== null ? "$menu_key='$menu_val'," : "$menu_key=null,";
                }
                $sql = substr($sql, 0,-1);
                $sql .= " WHERE id=$id;";
                mysql_query($sql);
            }else{
                $menu_key = array_keys($test_menu_arr_val);
                $menu_key_str = implode(',', $menu_key);
                $menu_val = array_values($test_menu_arr_val);
                $menu_val_str = '';
                if (is_array($menu_val) && count($menu_val) > 0)foreach ($menu_val as $menu_value){
                    $menu_val_str .= $menu_value !== null ? '"'.$menu_value.'",' : 'null,';
                }
                $menu_val_str = substr($menu_val_str, 0,-1);
                $sql = "INSERT INTO j_menu($menu_key_str) VALUES ($menu_val_str);";
                mysql_query($sql);
            }
        }
        $preview_auth_rule_arr_id = array_column($preview_auth_rule_arr, 'id');
        if (is_array($test_auth_rule_arr) && count($test_auth_rule_arr) > 0)foreach ($test_auth_rule_arr as $test_auth_rule_arr_val){
            $sql = '';
            if (in_array($test_auth_rule_arr_val['id'], $preview_auth_rule_arr_id)){
                $id = $test_auth_rule_arr_val['id'];
                $sql = "UPDATE j_auth_rule SET ";
                if (is_array($test_auth_rule_arr_val) && count($test_auth_rule_arr_val) > 0)foreach ($test_auth_rule_arr_val as $menu_key => $menu_val){
                    if ($menu_key == 'id'){
                        continue;
                    }
                    $sql .= $menu_val !== null ? "$menu_key='$menu_val'," : "$menu_key=null,";
                }
                $sql = substr($sql, 0,-1);
                $sql .= " WHERE id=$id;";
                mysql_query($sql);
            }else{
                $menu_key = array_keys($test_auth_rule_arr_val);
                $need_del_key = array_search('condition',$menu_key);
                unset($menu_key[$need_del_key]);
                $menu_key_str = implode(',', $menu_key);
                $menu_val = array_values($test_auth_rule_arr_val);
                $menu_val_str = '';
                if (is_array($test_auth_rule_arr_val) && count($test_auth_rule_arr_val) > 0)foreach ($test_auth_rule_arr_val as $menu_key=>$menu_value){
                    if ($menu_key == 'condition'){
                        continue;
                    }
                    $menu_val_str .= $menu_value !== null ? '"'.$menu_value.'",' : 'null,';
                }
                $menu_val_str = substr($menu_val_str, 0,-1);
                $sql = "INSERT INTO j_auth_rule($menu_key_str) VALUES ($menu_val_str);";
                mysql_query($sql);
            }
        }
        mysql_close($con);
        $this->retJson('菜单刷新成功',200);
    }
}